package com.brillilab.service.core.experiments.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.experiments.ExperimentStepReagentMapper;
import com.brillilab.domain.enums.kit.KitEnum;
import com.brillilab.domain.po.experiments.ExperimentStepReagent;
import com.brillilab.domain.vo.experiments.ExperimentStepReagentVo;
import com.brillilab.service.core.experiments.IExperimentStepReagentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 步骤的试剂
 * @author WuMenghao
 * @time   2019/04/22
 */
@Service
@Transactional
public class ExperimentStepReagentServiceImpl implements IExperimentStepReagentService {

    @Resource
    private ExperimentStepReagentMapper experimentStepReagentMapper;

    @Override
    public List<ExperimentStepReagent> insertBatch(List<ExperimentStepReagent> experimentStepReagents) {
        if(!CollectionUtils.isEmpty(experimentStepReagents)){
            experimentStepReagentMapper.insertBatch(experimentStepReagents);
        }
        return experimentStepReagents;
    }

    @Override
    public List<ExperimentStepReagent> selectKitReagentList(Long experimentId,Long phaseId,Long stepId) {
        return experimentStepReagentMapper.selectList(
                new LambdaQueryWrapper<ExperimentStepReagent>()
                        .eq(ExperimentStepReagent::getExperimentId,experimentId)
                        .eq(ExperimentStepReagent::getPhaseId,phaseId)
                        .eq(ExperimentStepReagent::getStepId,stepId)
                        .eq(ExperimentStepReagent::getClassify,KitEnum.Classify.REAGENT.getValue()));
    }

    @Override
    public List<ExperimentStepReagent> selectSampleReagentList(Long experimentId,Long phaseId,Long stepId) {
        return experimentStepReagentMapper.selectList(
                new LambdaQueryWrapper<ExperimentStepReagent>()
                        .eq(ExperimentStepReagent::getExperimentId,experimentId)
                        .eq(ExperimentStepReagent::getPhaseId,phaseId)
                        .eq(ExperimentStepReagent::getStepId,stepId)
                        .eq(ExperimentStepReagent::getClassify,KitEnum.Classify.SAMPLE.getValue()));
    }

    @Override
    public List<ExperimentStepReagentVo> selectExperimentStepReagentVoList(Long experimentId,Long phaseId,Long stepId) {
        List<ExperimentStepReagentVo> experimentStepReagentVos=experimentStepReagentMapper.selectExperimentStepReagentVoList(experimentId,phaseId,stepId);
        return experimentStepReagentVos!=null ? experimentStepReagentVos : new ArrayList<>();
    }

    @Override
    public List<ExperimentStepReagent> selectExperimentStepReagentList(Long experimentId,Long phaseId,Long stepId) {
        return experimentStepReagentMapper.selectList(
                new LambdaQueryWrapper<ExperimentStepReagent>()
                        .eq(ExperimentStepReagent::getExperimentId,experimentId)
                        .eq(ExperimentStepReagent::getPhaseId,phaseId)
                        .eq(ExperimentStepReagent::getStepId,stepId));
    }

    @Override
    public ExperimentStepReagent selectById(Long stepReagentId) {
        return experimentStepReagentMapper.selectById(stepReagentId);
    }

    @Override
    public int updateBatch(List<ExperimentStepReagent> reagentUpdate) {
        if(reagentUpdate.size()>0){
            return experimentStepReagentMapper.updateBatch(reagentUpdate);
        }
        return 0;
    }

    @Override
    public List<ExperimentStepReagent> selectList(List<Long> stepReagentIds) {
        if(stepReagentIds.size()>0){
            return experimentStepReagentMapper.selectList(
                    new LambdaQueryWrapper<ExperimentStepReagent>()
                            .in(ExperimentStepReagent::getId,stepReagentIds));
        }
        return new ArrayList<>(0);
    }

    //****    XuZue start   ****//
    @Override
    public void updateById(ExperimentStepReagent reagent){
        int i=experimentStepReagentMapper.updateById(reagent);
        Assert.isTrue(i==1,"操作失败！");
    }

    @Override
    public boolean deleteById(Long id){
        return experimentStepReagentMapper.deleteById(id) >0;
    }

    @Override
    public int selectCount(Long experimentId,Long phaseId,Long stepId) {
        return experimentStepReagentMapper.selectCount
                (new LambdaQueryWrapper<ExperimentStepReagent>()
                        .eq(ExperimentStepReagent::getExperimentId,experimentId)
                        .eq(ExperimentStepReagent::getPhaseId,phaseId)
                        .eq(ExperimentStepReagent::getStepId,stepId));
    }
    //****    end   ****//

	@Override
	public List<ExperimentStepReagent> selectStepReagentList(Long experimentId) {
		 return experimentStepReagentMapper.selectList(
	                new LambdaQueryWrapper<ExperimentStepReagent>()
	                        .eq(ExperimentStepReagent::getExperimentId,experimentId));
	}

	@Override
	public void deleteByIds(List<Long> idList) {
        if(idList!=null && idList.size()>0){
            experimentStepReagentMapper.deleteBatchIds(idList);
        }
	}

    @Override
    public List<ExperimentStepReagent> selectByUuidAndStepId(String uuid,Long stepId,Integer type) {
        return experimentStepReagentMapper.selectList(
                new LambdaQueryWrapper<ExperimentStepReagent>()
                        .eq(ExperimentStepReagent::getUuid,uuid)
                        .eq(ExperimentStepReagent::getStepId,stepId)
                        .eq(ExperimentStepReagent::getType,type));
    }

    @Override
    public boolean deleteByExperimentId(Long experimentId) {
        return experimentStepReagentMapper.delete(
                new LambdaQueryWrapper<ExperimentStepReagent>()
                        .eq(ExperimentStepReagent::getExperimentId,experimentId))>0;
    }

    @Override
    public List<ExperimentStepReagent> selectByReagentIdAndExperimentId(Long reagentId,Long experimentId) {
        return null;
    }
}
